Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./year2.RDS")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 533)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 11.37565 11.43165 11.48668 11.54075 11.59385 11.64599 11.69718 11.74741
## [9] 11.79669 11.84501 11.89239 11.93881 11.98429 12.02883 12.07243 12.11509
## [17] 12.15681 12.19759 12.23745 12.27637 12.31434 12.35134 12.38736 12.42242
## [25] 12.45650 12.48961 12.52175 12.55293 12.58313 12.61237 12.64064 12.66794
## [33] 12.69428 12.71966 12.74407 12.76752 12.79000 12.81153 12.83210 12.85170
## [41] 12.87034 12.88802 12.90473 12.92048 12.93526 12.94908 12.96193 12.97381
## [49] 12.98473 12.99467 13.00365 13.01165 13.01869 13.02475 13.02984 13.03333
## [57] 13.03470 13.03416 13.03189 13.02806 13.02288 13.01653 13.00920 13.00107
## [65] 12.99233 12.98317 12.97377 12.96433 12.95503 12.94606 12.93510 12.92009
## [73] 12.90159 12.88016 12.85636 12.83075 12.80391 12.77638 12.74873 12.72153
## [81] 12.69534 12.67071 12.64821 12.62840 12.61185 12.59538 12.57582 12.55370
## [89] 12.52958 12.50401 12.47752 12.45068 12.42402 12.39809 12.37344 12.35062
## [97] 12.33018 12.31265 12.29860 12.28682 12.27573 12.26530 12.25550 12.24628
## [105] 12.23762 12.22948 12.22181 12.21460 12.20779 12.20136 12.19528 12.18950
## [113] 12.18398 12.17871 12.17363 12.16871 12.16393 12.15923 12.15494 12.15133
## [121] 12.14838 12.14605 12.14429 12.14308 12.14236 12.14210 12.14226 12.14281
## [129] 12.14370 12.14489 12.14635 12.14803 12.15019 12.15307 12.15664 12.16086
## [137] 12.16571 12.17115 12.17714 12.18366 12.19067 12.19814 12.20603 12.21431
## [145] 12.22296 12.23192 12.24118 12.25070 12.26045 12.27039 12.28049 12.29071
## [153] 12.30103 12.31342 12.32952 12.34878 12.37067 12.39464 12.42016 12.44668
## [161] 12.47367 12.50059 12.52689 12.55204 12.57550 12.59673 12.61518 12.63032
## [169] 12.64161 12.65156 12.66294 12.67557 12.68928 12.70391 12.71928 12.73522
## [177] 12.75155 12.76812 12.78474 12.80124 12.81746 12.83322 12.84835 12.86268
## [185] 12.87605 12.88826 12.89917 12.90859 12.91635 12.92229 12.92623 12.92800
## [193] 12.92743 12.92435 12.91858 12.90982 12.89813 12.88384 12.86733 12.84895
## [201] 12.82905 12.80800 12.78615 12.76385 12.74147 12.71936 12.69788 12.67739
## [209] 12.65824 12.63628 12.60769 12.57335 12.53411 12.49084 12.44442 12.39570
## [217] 12.34555 12.29484 12.24444 12.19521 12.14801 12.10372 12.06320 12.02732
## [225] 11.99694 11.96754 11.93450 11.89856 11.86046 11.82092 11.78069 11.74050
## [233] 11.70109 11.66318 11.62753 11.59486 11.56590 11.54140 11.52209 11.50659
## [241] 11.49299 11.48117 11.47104 11.46249 11.45541 11.44972 11.44530 11.44206
## [249] 11.43988 11.43867 11.43833 11.43876 11.43984 11.44149 11.44359 11.44604
## [257] 11.44875 11.45161 11.45658 11.46534 11.47735 11.49209 11.50902 11.52760
## [265] 11.54731 11.56761 11.58797 11.60786 11.62674 11.64408 11.65936 11.67202
## [273] 11.68441 11.69900 11.71552 11.73370 11.75326 11.77394 11.79544 11.81751
## [281] 11.83986 11.86222 11.88432 11.90588 11.92662 11.94628 11.96457 11.98123
## [289] 11.99741 12.01435 12.03193 12.04999 12.06840 12.08702 12.10570 12.12432
## [297] 12.14272 12.16077 12.17833 12.19526 12.21141 12.22666 12.24124 12.25551
## [305] 12.26948 12.28317 12.29659 12.30976 12.32269 12.33539 12.34789 12.36020
## [313] 12.37233 12.38430 12.39611 12.40780 12.41937 12.43083 12.44221 12.45351
## [321] 12.46475 12.47596 12.48713 12.49876 12.51120 12.52430 12.53790 12.55184
## [329] 12.56597 12.58013 12.59417 12.60792 12.62122 12.63393 12.64588 12.65692
## [337] 12.66688 12.67617 12.68529 12.69420 12.70290 12.71137 12.71959 12.72754
## [345] 12.73521 12.74258 12.74964 12.75636 12.76272 12.76872 12.77434 12.77955
## [353] 12.78434 12.78870 12.79260 12.79603 12.79858 12.79994 12.80028 12.79975
## [361] 12.79852 12.79674 12.79458 12.79219 12.78974 12.78738 12.78527 12.78357
## [369] 12.78245 12.78206 12.78127 12.77899 12.77546 12.77089 12.76551 12.75955
## [377] 12.75323 12.74677 12.74041 12.73436 12.72885 12.72411 12.72036 12.71782
## [385] 12.71673 12.71730 12.71930 12.72223 12.72594 12.73028 12.73509 12.74023
## [393] 12.74554 12.75087 12.75606 12.76098 12.76545 12.76933 12.77247 12.77472
## [401] 12.77685 12.77969 12.78313 12.78706 12.79140 12.79602 12.80085 12.80576
## [409] 12.81065 12.81544 12.82001 12.82426 12.82809 12.83140 12.83408 12.83603
## [417] 12.83716 12.83736 12.83652 12.83500 12.83324 12.83125 12.82907 12.82671
## [425] 12.82421 12.82158 12.81885 12.81604 12.81318 12.81029 12.80739 12.80452
## [433] 12.80168 12.79830 12.79382 12.78837 12.78205 12.77496 12.76722 12.75891
## [441] 12.75017 12.74108 12.73176 12.72231 12.71285 12.70346 12.69428 12.68539
## [449] 12.67691 12.66894 12.66159 12.65496 12.64917 12.64432 12.63906 12.63218
## [457] 12.62399 12.61479 12.60488 12.59458 12.58418 12.57400 12.56433 12.55549
## [465] 12.54777 12.54149 12.53695 12.53445 12.53357 12.53362 12.53452 12.53617
## [473] 12.53847 12.54135 12.54472 12.54847 12.55252 12.55679 12.56117 12.56559
## [481] 12.56994 12.57415 12.57811 12.58174 12.58546 12.58969 12.59440 12.59955
## [489] 12.60508 12.61096 12.61713 12.62357 12.63021 12.63703 12.64396 12.65098
## [497] 12.65803 12.66507 12.67224 12.67969 12.68741 12.69540 12.70366 12.71218
## [505] 12.72096 12.72999 12.73926 12.74878 12.75854 12.76852 12.77874 12.78918
## [513] 12.79985 12.81072 12.82181 12.83310 12.84459 12.85627 12.86815 12.88020
## [521] 12.89242 12.90482 12.91740 12.93017 12.94314 12.95631 12.96969 12.98328
## [529] 12.99710 13.01116 13.02544 13.03998 13.05476
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 533)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 10.92978 11.00067 11.07044 11.13906 11.20653 11.27284 11.33796 11.40190
## [9] 11.46463 11.52615 11.58644 11.64549 11.70328 11.75982 11.81507 11.86904
## [17] 11.92171 11.97306 12.02308 12.07177 12.11914 12.16521 12.21002 12.25357
## [25] 12.29589 12.33700 12.37690 12.41563 12.45319 12.48961 12.52491 12.55909
## [33] 12.59219 12.62421 12.65519 12.68512 12.71373 12.74075 12.76623 12.79022
## [41] 12.81280 12.83401 12.85391 12.87255 12.89000 12.90631 12.92154 12.93573
## [49] 12.94896 12.96128 12.97274 12.98340 12.99332 13.00255 13.01115 13.01806
## [57] 13.02237 13.02434 13.02422 13.02229 13.01879 13.01399 13.00815 13.00154
## [65] 12.99440 12.98702 12.97964 12.97252 12.96593 12.96013 12.95418 12.94702
## [73] 12.93874 12.92944 12.91920 12.90811 12.89627 12.88376 12.87068 12.85712
## [81] 12.84316 12.82891 12.81444 12.79985 12.78523 12.76865 12.74851 12.72538
## [89] 12.69982 12.67240 12.64368 12.61424 12.58465 12.55546 12.52725 12.50058
## [97] 12.47603 12.45415 12.43553 12.41757 12.39752 12.37571 12.35242 12.32798
## [105] 12.30268 12.27684 12.25077 12.22477 12.19915 12.17422 12.15028 12.12765
## [113] 12.10663 12.08754 12.07067 12.05634 12.04485 12.03651 12.02941 12.02157
## [121] 12.01325 12.00469 11.99612 11.98781 11.97998 11.97289 11.96678 11.96190
## [129] 11.95848 11.95678 11.95704 11.95950 11.96453 11.97221 11.98231 11.99462
## [137] 12.00894 12.02504 12.04271 12.06174 12.08193 12.10304 12.12487 12.14721
## [145] 12.16985 12.19256 12.21514 12.23738 12.25905 12.27996 12.29987 12.31859
## [153] 12.33589 12.35532 12.38007 12.40937 12.44245 12.47856 12.51692 12.55677
## [161] 12.59736 12.63791 12.67766 12.71585 12.75172 12.78449 12.81341 12.83771
## [169] 12.85663 12.87309 12.89042 12.90846 12.92709 12.94616 12.96553 12.98504
## [177] 13.00457 13.02396 13.04308 13.06177 13.07990 13.09732 13.11390 13.12948
## [185] 13.14393 13.15710 13.16884 13.17902 13.18749 13.19412 13.19874 13.20123
## [193] 13.20144 13.19923 13.19444 13.18703 13.17719 13.16518 13.15126 13.13568
## [201] 13.11870 13.10058 13.08158 13.06195 13.04195 13.02185 13.00189 12.98233
## [209] 12.96344 12.94217 12.91574 12.88478 12.84995 12.81187 12.77118 12.72852
## [217] 12.68453 12.63984 12.59510 12.55093 12.50799 12.46690 12.42830 12.39284
## [225] 12.36114 12.32896 12.29215 12.25156 12.20801 12.16235 12.11541 12.06802
## [233] 12.02101 11.97522 11.93149 11.89065 11.85353 11.82097 11.79380 11.76922
## [241] 11.74398 11.71829 11.69233 11.66630 11.64039 11.61480 11.58972 11.56535
## [249] 11.54187 11.51949 11.49839 11.47877 11.46082 11.44475 11.43073 11.41897
## [257] 11.40966 11.40299 11.39952 11.39941 11.40227 11.40770 11.41531 11.42471
## [265] 11.43549 11.44727 11.45964 11.47222 11.48461 11.49642 11.50725 11.51670
## [273] 11.52705 11.54058 11.55692 11.57571 11.59657 11.61913 11.64303 11.66788
## [281] 11.69333 11.71900 11.74452 11.76951 11.79362 11.81647 11.83768 11.85689
## [289] 11.87661 11.89925 11.92436 11.95146 11.98009 12.00976 12.04003 12.07040
## [297] 12.10042 12.12961 12.15751 12.18364 12.20754 12.22873 12.24852 12.26849
## [305] 12.28859 12.30878 12.32902 12.34925 12.36943 12.38951 12.40946 12.42921
## [313] 12.44874 12.46798 12.48690 12.50545 12.52358 12.54124 12.55840 12.57500
## [321] 12.59100 12.60636 12.62102 12.63501 12.64844 12.66135 12.67381 12.68586
## [329] 12.69756 12.70897 12.72013 12.73111 12.74196 12.75273 12.76349 12.77427
## [337] 12.78514 12.79581 12.80598 12.81567 12.82493 12.83378 12.84226 12.85039
## [345] 12.85822 12.86577 12.87307 12.88017 12.88708 12.89385 12.90051 12.90708
## [353] 12.91361 12.92011 12.92664 12.93321 12.93956 12.94546 12.95097 12.95613
## [361] 12.96100 12.96566 12.97015 12.97453 12.97886 12.98320 12.98761 12.99215
## [369] 12.99687 13.00183 13.00692 13.01200 13.01705 13.02206 13.02702 13.03193
## [377] 13.03679 13.04157 13.04627 13.05089 13.05542 13.05985 13.06416 13.06836
## [385] 13.07243 13.07637 13.08073 13.08596 13.09188 13.09829 13.10503 13.11189
## [393] 13.11871 13.12529 13.13145 13.13701 13.14179 13.14559 13.14824 13.14954
## [401] 13.15006 13.15041 13.15060 13.15059 13.15035 13.14988 13.14914 13.14812
## [409] 13.14679 13.14512 13.14310 13.14071 13.13792 13.13471 13.13105 13.12693
## [417] 13.12232 13.11719 13.11154 13.10454 13.09561 13.08504 13.07315 13.06022
## [425] 13.04656 13.03249 13.01828 13.00426 12.99072 12.97796 12.96629 12.95600
## [433] 12.94741 12.93913 12.92969 12.91922 12.90787 12.89576 12.88304 12.86984
## [441] 12.85630 12.84257 12.82876 12.81504 12.80152 12.78836 12.77568 12.76362
## [449] 12.75233 12.74193 12.73258 12.72439 12.71752 12.71210 12.70754 12.70318
## [457] 12.69902 12.69511 12.69145 12.68808 12.68500 12.68225 12.67985 12.67781
## [465] 12.67617 12.67493 12.67413 12.67378 12.67408 12.67517 12.67698 12.67944
## [473] 12.68246 12.68598 12.68991 12.69420 12.69876 12.70352 12.70841 12.71335
## [481] 12.71827 12.72310 12.72776 12.73218 12.73669 12.74164 12.74699 12.75273
## [489] 12.75881 12.76521 12.77189 12.77883 12.78598 12.79333 12.80082 12.80845
## [497] 12.81617 12.82395 12.83189 12.84011 12.84861 12.85738 12.86641 12.87570
## [505] 12.88525 12.89504 12.90507 12.91534 12.92584 12.93657 12.94752 12.95868
## [513] 12.97005 12.98162 12.99339 13.00535 13.01749 13.02982 13.04232 13.05499
## [521] 13.06783 13.08085 13.09404 13.10742 13.12100 13.13477 13.14875 13.16295
## [529] 13.17737 13.19201 13.20689 13.22200 13.23736
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 533)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 10.88993 10.94700 11.00316 11.05839 11.11269 11.16606 11.21848 11.26997
## [9] 11.32050 11.37009 11.41872 11.46639 11.51310 11.55884 11.60361 11.64741
## [17] 11.69022 11.73205 11.77290 11.81275 11.85163 11.88953 11.92647 11.96245
## [25] 11.99748 12.03155 12.06467 12.09684 12.12807 12.15836 12.18771 12.21613
## [33] 12.24362 12.27018 12.29582 12.32055 12.34419 12.36664 12.38792 12.40806
## [41] 12.42710 12.44507 12.46201 12.47795 12.49292 12.50696 12.52011 12.53239
## [49] 12.54384 12.55450 12.56439 12.57356 12.58203 12.58984 12.59703 12.60300
## [57] 12.60725 12.60991 12.61116 12.61112 12.60996 12.60782 12.60486 12.60122
## [65] 12.59706 12.59253 12.58777 12.58293 12.57818 12.57365 12.56769 12.55883
## [73] 12.54744 12.53393 12.51866 12.50205 12.48447 12.46631 12.44796 12.42982
## [81] 12.41227 12.39570 12.38050 12.36705 12.35576 12.34471 12.33193 12.31769
## [89] 12.30228 12.28597 12.26905 12.25179 12.23447 12.21737 12.20078 12.18496
## [97] 12.17021 12.15679 12.14500 12.13398 12.12275 12.11135 12.09979 12.08813
## [105] 12.07640 12.06463 12.05285 12.04111 12.02943 12.01786 12.00642 11.99516
## [113] 11.98411 11.97330 11.96276 11.95255 11.94268 11.93320 11.92316 11.91181
## [121] 11.89943 11.88631 11.87274 11.85901 11.84541 11.83223 11.81975 11.80827
## [129] 11.79808 11.78946 11.78271 11.77810 11.77452 11.77069 11.76670 11.76265
## [137] 11.75862 11.75471 11.75101 11.74761 11.74460 11.74207 11.74011 11.73882
## [145] 11.73829 11.73861 11.73986 11.74215 11.74556 11.75018 11.75611 11.76343
## [153] 11.77224 11.78369 11.79856 11.81641 11.83679 11.85927 11.88338 11.90870
## [161] 11.93476 11.96113 11.98737 12.01301 12.03763 12.06077 12.08198 12.10083
## [169] 12.11687 12.13304 12.15241 12.17466 12.19949 12.22661 12.25571 12.28649
## [177] 12.31865 12.35188 12.38588 12.42035 12.45500 12.48951 12.52358 12.55692
## [185] 12.58922 12.62017 12.64949 12.67686 12.70198 12.72455 12.74427 12.76084
## [193] 12.77395 12.78330 12.78860 12.79014 12.78869 12.78458 12.77818 12.76983
## [201] 12.75989 12.74872 12.73665 12.72406 12.71128 12.69867 12.68659 12.67538
## [209] 12.66540 12.65275 12.63384 12.60946 12.58040 12.54745 12.51142 12.47308
## [217] 12.43323 12.39267 12.35218 12.31257 12.27463 12.23914 12.20690 12.17871
## [225] 12.15536 12.13333 12.10890 12.08252 12.05464 12.02571 11.99618 11.96651
## [233] 11.93714 11.90854 11.88114 11.85541 11.83179 11.81074 11.79271 11.77640
## [241] 11.76024 11.74432 11.72869 11.71344 11.69862 11.68430 11.67056 11.65746
## [249] 11.64508 11.63347 11.62272 11.61289 11.60404 11.59625 11.58958 11.58411
## [257] 11.57991 11.57703 11.57646 11.57884 11.58380 11.59094 11.59986 11.61018
## [265] 11.62150 11.63344 11.64560 11.65760 11.66903 11.67952 11.68866 11.69607
## [273] 11.70324 11.71183 11.72165 11.73255 11.74436 11.75690 11.77002 11.78355
## [281] 11.79731 11.81115 11.82488 11.83836 11.85140 11.86384 11.87552 11.88626
## [289] 11.89696 11.90854 11.92086 11.93379 11.94720 11.96096 11.97494 11.98902
## [297] 12.00305 12.01692 12.03048 12.04361 12.05618 12.06805 12.08017 12.09346
## [305] 12.10779 12.12300 12.13896 12.15552 12.17256 12.18991 12.20745 12.22503
## [313] 12.24251 12.25975 12.27660 12.29293 12.30860 12.32346 12.33737 12.35019
## [321] 12.36178 12.37200 12.38070 12.38784 12.39358 12.39810 12.40157 12.40418
## [329] 12.40609 12.40749 12.40855 12.40946 12.41038 12.41150 12.41299 12.41503
## [337] 12.41781 12.42066 12.42287 12.42450 12.42561 12.42628 12.42655 12.42649
## [345] 12.42616 12.42563 12.42497 12.42422 12.42345 12.42274 12.42213 12.42169
## [353] 12.42149 12.42159 12.42204 12.42292 12.42332 12.42244 12.42050 12.41771
## [361] 12.41430 12.41047 12.40644 12.40243 12.39866 12.39533 12.39267 12.39090
## [369] 12.39022 12.39085 12.39226 12.39375 12.39534 12.39703 12.39883 12.40075
## [377] 12.40281 12.40499 12.40733 12.40981 12.41246 12.41528 12.41827 12.42145
## [385] 12.42483 12.42841 12.43313 12.43973 12.44788 12.45726 12.46755 12.47843
## [393] 12.48958 12.50068 12.51140 12.52142 12.53043 12.53810 12.54411 12.54815
## [401] 12.55123 12.55457 12.55810 12.56178 12.56554 12.56932 12.57307 12.57673
## [409] 12.58024 12.58355 12.58659 12.58931 12.59166 12.59357 12.59498 12.59584
## [417] 12.59610 12.59569 12.59455 12.59217 12.58823 12.58295 12.57657 12.56933
## [425] 12.56146 12.55318 12.54475 12.53638 12.52832 12.52080 12.51405 12.50830
## [433] 12.50379 12.49952 12.49439 12.48849 12.48191 12.47476 12.46713 12.45911
## [441] 12.45079 12.44228 12.43367 12.42505 12.41652 12.40817 12.40010 12.39240
## [449] 12.38517 12.37850 12.37249 12.36724 12.36283 12.35936 12.35653 12.35393
## [457] 12.35158 12.34944 12.34751 12.34578 12.34423 12.34285 12.34163 12.34056
## [465] 12.33962 12.33881 12.33811 12.33750 12.33712 12.33708 12.33736 12.33791
## [473] 12.33872 12.33975 12.34098 12.34237 12.34390 12.34553 12.34725 12.34901
## [481] 12.35080 12.35257 12.35431 12.35598 12.35769 12.35958 12.36162 12.36383
## [489] 12.36620 12.36872 12.37139 12.37420 12.37715 12.38024 12.38346 12.38681
## [497] 12.39028 12.39387 12.39760 12.40148 12.40552 12.40971 12.41405 12.41854
## [505] 12.42318 12.42796 12.43288 12.43794 12.44315 12.44849 12.45396 12.45957
## [513] 12.46531 12.47118 12.47718 12.48330 12.48954 12.49591 12.50240 12.50901
## [521] 12.51575 12.52263 12.52964 12.53679 12.54407 12.55149 12.55904 12.56673
## [529] 12.57457 12.58254 12.59065 12.59891 12.60731
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")
keeping in case
#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")